import { ChatPrivateData } from "./ChatPrivateData";
import { EntityUtil } from "../../../scene/layer/utils/EntityUtil";
import { PlayerMiniInfo } from "../../../model/PlayerMiniInfo";
import { SoundManager } from "../../../../common/sound/SoundManager";
import { SoundTypeConst } from "../../../../common/sound/SoundTypeConst";
import { ChatWindow } from "./ChatWindow";
import { WindowEvent } from "../../../../component/window/WindowEvent";
import { Global } from "../../../../../com/gengine/global/Global";
import { ChatGuildWindow } from "../chatGuild/ChatGuildWindow";
import { BaseWindow } from "../../../../component/window/BaseWindow";
import { SEntityId } from "../../../../../Message/Public/SEntityId";
import { ChatHintIconView } from "./ChatHintIconView";
import { Cache } from "../../../cache/Cache";
import { ChatPublicCrossWindow } from "../chatCross/ChatPublicCrossWindow";
import { Vector } from "../../../../../__AS3__/vec/Vector";
//class ChatManager
    
    export  class ChatManager
    {
        

        public static addMessage(arg1: ChatPrivateData): void
        {
            var loc1=ChatManager.getWindowBySEntityId(arg1.chatWindowEntityId);
            var loc2=arg1.sChatMsg;
            var loc3=arg1.chatWindowName;
            var loc4=arg1.date;
            var loc5=arg1.content;
            var loc6=arg1.chatColor;
            var loc7=arg1.chatFromPlayerName;
            if (loc2 && loc1 && loc3 == loc2.fromPlayer.name && EntityUtil.equal(arg1.chatWindowEntityId, arg1.sChatMsg.fromPlayer.entityId)) 
            {
                loc1.updatePlayerMsg(new PlayerMiniInfo(loc2.fromPlayer));
            }
            if (loc1) 
            {
                loc1.addChatItem(loc7, loc4, loc5, loc6, arg1.faceAuthority);
                if (!loc1.visible || loc1.isHide) 
                {
                    loc1.icon.isFlicker = true;
                    SoundManager.instance.soundPlay(SoundTypeConst.InformationPresentation);
                }
            }
            return;
        }

        public static addWindow(arg1: PlayerMiniInfo): ChatWindow
        {
            var playerInfo: PlayerMiniInfo;
            var objWindow: ChatWindow;
            var CloseHandler: Function;
            var showOrHide: Function;

            var loc1;
            objWindow = null;
            CloseHandler = null;
            showOrHide = null;
            playerInfo = arg1;
            CloseHandler = (arg1: WindowEvent): void
            {
                ChatManager._chatHintIconView.removeIcon(objWindow.icon);
                objWindow.icon.removeCall(showOrHide);
                ChatManager.removeChatWindow(objWindow);
                return;
            }
            showOrHide = (arg1: Object): void
            {
                objWindow.visible = !objWindow.visible;
                objWindow.show();
                if (objWindow.visible && Global.stage.contains(objWindow)) 
                {
                    objWindow.inputText.setFocus();
                }
                return;
            }
            objWindow = ChatManager.getWindowBySEntityId(playerInfo.entityId);
            if (!objWindow) 
            {
                objWindow = new ChatWindow(playerInfo);
                objWindow.visible = false;
                ChatManager._aryWindow.push(objWindow);
            }
            if (!ChatManager._chatHintIconView.hasHincIcon(objWindow.icon)) 
            {
                ChatManager._chatHintIconView.addIcon(objWindow.icon);
                objWindow.addEventListener(WindowEvent.CLOSE, CloseHandler);
                objWindow.icon.addCall(showOrHide);
            }
            return objWindow;
        }

        public static addGuildChatWindow(arg1: Boolean=false): ChatGuildWindow
        {
            var isForce: Boolean=false;
            var CloseHandler: Function;
            var showOrHide: Function;

            var loc1;
            CloseHandler = null;
            showOrHide = null;
            isForce = arg1;
            CloseHandler = (arg1: WindowEvent): void
            {
                ChatManager._chatHintIconView.removeIcon(ChatManager._chatGuildWindow.icon);
                ChatManager._chatGuildWindow.icon.removeCall(showOrHide);
                ChatManager.removeChatWindow(ChatManager._chatGuildWindow);
                return;
            }
            showOrHide = (arg1: Object): void
            {
                ChatManager._chatGuildWindow.visible = !ChatManager._chatGuildWindow.visible;
                ChatManager._chatGuildWindow.show();
                return;
            }
            if (!ChatManager._chatGuildWindow) 
            {
                ChatManager._chatGuildWindow = ChatGuildWindow.instance;
                ChatManager._chatGuildWindow.visible = false;
            }
            if (!ChatManager._chatGuildWindow.isShowWindow && !isForce) 
            {
                return ChatManager._chatGuildWindow;
            }
            if (!ChatManager._chatHintIconView.hasHincIcon(ChatManager._chatGuildWindow.icon)) 
            {
                ChatManager._chatHintIconView.addIcon(ChatManager._chatGuildWindow.icon);
                ChatManager._chatGuildWindow.addEventListener(WindowEvent.CLOSE, CloseHandler);
                ChatManager._chatGuildWindow.icon.addCall(showOrHide);
            }
            if (isForce) 
            {
                ChatManager._chatGuildWindow.visible = true;
                ChatManager._chatGuildWindow.show();
            }
            return ChatManager._chatGuildWindow;
        }

        public static removeGuildChatWindow(): void
        {
            if (ChatManager._chatGuildWindow) 
            {
                ChatManager._chatHintIconView.removeIcon(ChatManager._chatGuildWindow.icon);
                ChatManager._chatGuildWindow.clearAndHide();
                ChatManager.removeChatWindow(ChatManager._chatGuildWindow);
            }
            return;
        }

        public static addGuildChatMsg(arg1: PlayerMiniInfo, arg2: Date, arg3: string, arg4: Array<any>, arg5: Array<any>, arg6: uint=0, arg7: Boolean=false): void
        {
            var loc1;
            if (loc1 = ChatManager._chatGuildWindow) 
            {
                loc1.addChatItem(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
                if (!(!loc1.visible || loc1.isHide)) 
                {
                };
            }
            return;
        }

        /* internal  */static removeChatWindow(arg1: BaseWindow): void
        {
            arg1.visible = false;
            return;
        }

        public static getWindowByName(arg1: string): ChatWindow
        {
            var loc1=null;
            var loc2=0;
            var loc3=ChatManager._aryWindow;
            for(loc1 of loc3) 
            {
                if (loc1.getChatPlayerName() != arg1) 
                {
                    continue;
                }
                return loc1;
            }
            return null;
        }

        public static getWindowBySEntityId(arg1: SEntityId): ChatWindow
        {
            var loc1=null;
            if (!arg1) 
            {
                return null;
            }
            var loc2=0;
            var loc3=ChatManager._aryWindow;
            for(loc1 of loc3) 
            {
                if (!EntityUtil.equal(loc1.getChatPlayerEntityId(), arg1)) 
                {
                    continue;
                }
                return loc1;
            }
            return null;
        }

        public static closeAllOtherSeverChatWindw(arg1: ChatHintIconView): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=ChatManager._aryWindow;
            for(loc1 of loc3) 
            {
                if (EntityUtil.isSameServer(loc1.getChatPlayerEntityId(), Cache.instance.role.entityInfo.entityId)) 
                {
                    continue;
                }
                loc1.visible = false;
                arg1.removeIcon(loc1.icon);
                loc1.icon.removeAllCall();
            }
            return;
        }

        public static addPublicCrossChatMsg(arg1: PlayerMiniInfo, arg2: Date, arg3: string, arg4: Array<any>, arg5: Array<any>, arg6: uint=0, arg7: Boolean=false): void
        {
            var loc1;
            if (loc1 = ChatManager._chatPublicCrossWindow) 
            {
                loc1.addChatItem(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
                if (!(!loc1.visible || loc1.isHide)) 
                {
                };
            }
            return;
        }

        public static addPublicCrossWindow(arg1: Boolean=false): ChatPublicCrossWindow
        {
            var isForce: Boolean=false;
            var CloseHandler: Function;
            var showOrHide: Function;

            var loc1;
            CloseHandler = null;
            showOrHide = null;
            isForce = arg1;
            CloseHandler = (arg1: WindowEvent): void
            {
                ChatManager._chatHintIconView.removeIcon(ChatManager._chatPublicCrossWindow.icon);
                ChatManager._chatPublicCrossWindow.icon.removeCall(showOrHide);
                ChatManager.removeChatWindow(ChatManager._chatPublicCrossWindow);
                return;
            }
            showOrHide = (arg1: Object): void
            {
                ChatManager._chatPublicCrossWindow.visible = !ChatManager._chatPublicCrossWindow.visible;
                ChatManager._chatPublicCrossWindow.show();
                return;
            }
            if (!ChatManager._chatPublicCrossWindow) 
            {
                ChatManager._chatPublicCrossWindow = ChatPublicCrossWindow.instance;
                ChatManager._chatPublicCrossWindow.visible = false;
            }
            if (!ChatManager._chatPublicCrossWindow.isShowWindow && !isForce) 
            {
                return ChatManager._chatPublicCrossWindow;
            }
            if (!ChatManager._chatHintIconView.hasHincIcon(ChatManager._chatPublicCrossWindow.icon)) 
            {
                ChatManager._chatHintIconView.addIcon(ChatManager._chatPublicCrossWindow.icon);
                ChatManager._chatPublicCrossWindow.addEventListener(WindowEvent.CLOSE, CloseHandler);
                ChatManager._chatPublicCrossWindow.icon.addCall(showOrHide);
            }
            if (isForce) 
            {
                ChatManager._chatPublicCrossWindow.visible = true;
                ChatManager._chatPublicCrossWindow.show();
            }
            return ChatManager._chatPublicCrossWindow;
        }

        public static removePublicCrossChatWindow(): void
        {
            if (ChatManager._chatPublicCrossWindow) 
            {
                ChatManager._chatHintIconView.removeIcon(ChatManager._chatPublicCrossWindow.icon);
                ChatManager._chatPublicCrossWindow.clearAndHide();
                ChatManager.removeChatWindow(ChatManager._chatPublicCrossWindow);
            }
            return;
        }

        public static addHintIcon(arg1: Object /* flash.display.Sprite */): void
        {
            ChatManager._chatHintIconView.addIcon(arg1);
            return;
        }

        public static removeHintIcon(arg1: Object /* flash.display.Sprite */): void
        {
            ChatManager._chatHintIconView.removeIcon(arg1);
            return;
        }

        /* internal */ static /* var */_aryWindow: Vector.<ChatWindow>;

        /* internal */ static /* var */_chatHintIconView: ChatHintIconView;

        /* internal */ static /* var */_chatGuildWindow: ChatGuildWindow;

        /* internal */ static /* var */_chatPublicCrossWindow: ChatPublicCrossWindow;
    }

            ChatManager._aryWindow = new Vector.<ChatWindow>();
            ChatManager._chatHintIconView = ChatHintIconView.instance();
        